#include <bitset>
#include <cstdint>
#include <iostream>
using ll = int64_t;

const ll maxn = 1e5+5;
const ll T=1e5+3,F=-1e5-3,U=0;
ll c,t,n,m,f[maxn];
std::bitset<2*maxn> vis;
#define vis(x)vis[(x)+n]

static inline ll find(ll x){
    ll ret;
    if(x==T||x==F)ret=x;
    else if(vis(-x)||x==U){
        ret=U;
    }else if(vis(x))ret=T;
    else if(x<0){
        if(-f[-x] == x)ret=x;
        else{
            vis(x)=true;
            f[-x]=ret=find(-f[-x]);
            vis(x)=false;
        }
    }else{
        if(f[x]==x)ret=x;
        else{
            vis(x)=true;
            f[x]=ret=find(f[x]);
            vis(x)=false;
        }
    }
    return ret;
}

static inline void solve(){
    vis.reset();
    std::cin>>n>>m;
    ll ans=0;
    for(ll i=1;i<=n;i++)f[i]=i;
    for(ll i=1;i<=m;i++){
        char c;
        std::cin>>c;
        if(c=='T' || c=='F' || c=='U'){
            ll v;
            std::cin>>v;
            switch (c) {
                case 'T':f[v]=T;break;
                case 'F':f[v]=F;break;
                case 'U':f[v]=U;break;
            }
        }else if(c=='+'){
            ll i,j;
            std::cin>>i>>j;
            f[i]=f[j];
        }else if(c=='-'){
            ll i,j;
            std::cin>>i>>j;
            f[i]=-f[j];
        }
    }
    for(ll i=1;i<=n;i++){
        ans+=find(i)==U;
    }
    std::cout<<ans<<"\n";
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>c>>t;
    while (t--) {
        solve();
    }
}
